package bb.lanxing.calc.calculator;

import bb.lanxing.calc.data.BiketrainerPoint;
import android.util.Log;
import bb.lanxing.util.CommonUtil;
import com.github.mikephil.charting.utils.Utils;

public class BiketrainerCalc {
    final static private String TAG = "BiketrainerCalc";
    private BiketrainerPoint lastBiketrainerPoint;
    private long lastValidBikeTrainerTime;
    public boolean calc(BiketrainerPoint biketrainerPoint) {
        int cadence = 0;

        BiketrainerPoint biketrainerPoint2 = this.lastBiketrainerPoint;
        if (biketrainerPoint2 == null) {
            this.lastBiketrainerPoint = biketrainerPoint;
            return false;
        }
        int sumCrank = biketrainerPoint2.getBiketrainerCrank();
        int sumCrank2 = biketrainerPoint.getBiketrainerCrank();
        int eventTime = this.lastBiketrainerPoint.getBiketrainerEventTime();
        int eventTime2 = biketrainerPoint.getBiketrainerEventTime();
        Log.v(TAG, "BiketrainerCalc, sumCrank = " + sumCrank);
        Log.v(TAG, "BiketrainerCalc, sumCrank2 = " + sumCrank2);
        Log.v(TAG, "BiketrainerCalc, eventTime = " + eventTime);
        Log.v(TAG, "BiketrainerCalc, eventTime2 = " + eventTime2);

        int do16BitDiff = CommonUtil.do16BitDiff(sumCrank2, sumCrank);
        double do16BitDiff2 = CommonUtil.do16BitDiff(eventTime2, eventTime) / 1024.0f;
        if (eventTime > 0 && eventTime2 > 0 && do16BitDiff > 0 && do16BitDiff2 > Utils.DOUBLE_EPSILON) {
            this.lastValidBikeTrainerTime = System.currentTimeMillis();
            int ceil = (int) Math.ceil((do16BitDiff / do16BitDiff2) * 60.0d);
            int cadence2 = this.lastBiketrainerPoint.getBiketrainerCadence();
            if (cadence2 <= 256 && cadence2 >= 0) {
                int i2 = ceil - cadence2;
                if (i2 > 50) {
                    ceil = cadence2 + 50;
                }
                if (i2 < -50) {
                    ceil = cadence2 - 50;
                }
            }
            cadence = Math.min(ceil, 256);
        }else{
            if (System.currentTimeMillis() - this.lastValidBikeTrainerTime <= 3000) {
                cadence = this.lastBiketrainerPoint.getBiketrainerCadence();
            }else{
                cadence = 0;
            }
        }
        Log.v(TAG, "BiketrainerCalc, cadence = " + cadence);
        biketrainerPoint.setBiketrainerCadence(cadence);
        this.lastBiketrainerPoint = biketrainerPoint;
        return true;
    }
        public void release() {
        this.lastBiketrainerPoint = null;
        this.lastValidBikeTrainerTime = 0L;
    }
}